package com.xxxx.manager;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.xxxx.config.DataSourceConfig;
import com.xxxx.persistence.domain.UserDO;
import com.xxxx.persistence.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * @ClassName: UserManager
 * @Description: 用户管理器
 * @Author: ZhouWei
 * @Date: 2023-4-20 - 17:10
 */
@Service
public class UserManager {

    @Autowired
    private UserManager proxy;

    @Autowired
    private UserRepository userRepository;

    /**
     * 读库添加数据
     */
    public void insertReadDataBase(UserDO user) {
        userRepository.insert(user);
    }

    /**
     * 读库查询数据
     */
    public List<UserDO> listReadDataBase() {
        return userRepository.selectList(null);
    }

    /**
     * 写库添加数据
     */
    @DS(DataSourceConfig.SHARDING_DATA_SOURCE_NAME)
    public void insertWriteDataBase(UserDO user) {
        userRepository.insert(user);
    }

    /**
     * 删除所有库数据
     */
    public void deleteAllDataBase() {
        proxy.deleteReadDataBase();
        proxy.deleteWriteDataBase();
    }

    /**
     * 删除读库数据
     */
    public void deleteReadDataBase(){
        userRepository.delete(null);
    }

    /**
     * 删除写库数据
     */
    @DS(DataSourceConfig.SHARDING_DATA_SOURCE_NAME)
    public void deleteWriteDataBase() {
        userRepository.delete(null);
    }
}
